﻿// DataProductPropertyDdialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "DataProductPropertyDdialog.h"
#include "afxdialogex.h"


// CDataProductPropertyDdialog 对话框

IMPLEMENT_DYNAMIC(CDataProductPropertyDdialog, CDialogEx)

CDataProductPropertyDdialog::CDataProductPropertyDdialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_DATA_PRODUCT_PROPERTY, pParent)
{
	m_pSchema = NULL;
	m_pProduct = NULL;
}

CDataProductPropertyDdialog::~CDataProductPropertyDdialog()
{
}

void CDataProductPropertyDdialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MFCPROPERTYGRID1, m_props);
}


BEGIN_MESSAGE_MAP(CDataProductPropertyDdialog, CDialogEx)
END_MESSAGE_MAP()


// CDataProductPropertyDdialog 消息处理程序


BOOL CDataProductPropertyDdialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	m_props.EnableHeaderCtrl(TRUE, _T("属性"), _T("值"));
	//指定列宽
	HDITEM item;
	item.cxy = 200;
	item.mask = HDI_WIDTH;
	m_props.GetHeaderCtrl().SetItem(0, new HDITEM(item));

	// TODO:  在此添加额外的初始化
	if (m_pProduct)
	{
		ck::ckDataProductType type = m_pProduct->GetType();
		ck::DataProductManager* pManager = ck::DataProductManager::GetInstance();
		m_pSchema = pManager->GetSchema(type);
		if (m_pSchema)
		{
			Populate(m_pSchema);

		}
	}


	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CDataProductPropertyDdialog::SetProduct(ck::DataProductInfo* pProduct)
{
	m_pProduct = pProduct;
}

void CDataProductPropertyDdialog::Populate(ck::data::Schema* pSchema)
{
	m_props.RemoveAll();

	ck::data::Group* pGroup = NULL;
	ck::data::EnumGroup* pGroups = NULL;

	pGroups = pSchema->GetGroups();
	pGroups->Reset();
	while ((pGroup = pGroups->Next()))
	{
		const char* szName = pGroup->GetName();
		CMFCPropertyGridProperty* pxGroup = new CMFCPropertyGridProperty(szName);

		Populate(pGroup, pxGroup);

		m_props.AddProperty(pxGroup);
	}

	pGroups->Release();

	/*z_uint32 id = pProduct->GetID();
	z_uint32 code = pProduct->GetCode();
	const char* szType = pProduct->GetTypeAsString();
	const char* szName = pProduct->GetName();
	const char* szDescription = pProduct->GetDescription();

	CMFCPropertyGridProperty* pProjGroup = new CMFCPropertyGridProperty(_T("数据产品"));
	pProjGroup->AddSubItem(new CMFCPropertyGridProperty("名称", (_variant_t)szName, _T("")));
	pProjGroup->AddSubItem(new CMFCPropertyGridProperty("类型", (_variant_t)szType, _T("")));
	pProjGroup->AddSubItem(new CMFCPropertyGridProperty("描述", (_variant_t)szDescription, _T("")));*/
}

void CDataProductPropertyDdialog::Populate(ck::data::Group* pGroup, CMFCPropertyGridProperty* pxGroup)
{
	ck::data::Group* pGroup2 = NULL;
	ck::data::EnumGroup* pGroups = NULL;
	pGroups = pGroup->GetGroups();
	pGroups->Reset();
	while ((pGroup2 = pGroups->Next()))
	{
		const char* szName = pGroup2->GetName();
		CMFCPropertyGridProperty* pxGroup = new CMFCPropertyGridProperty(szName);

		Populate(pGroup2, pxGroup);

		pxGroup->AddSubItem(pxGroup);
	}
	pGroups->Release();

	ck::data::Field* pField = NULL;
	ck::data::EnumField* pFields = NULL;
	pFields = pGroup->GetFields();
	pFields->Reset();
	while ((pField = pFields->Next()))
	{
		const char* szName = pField->GetName();
		CMFCPropertyGridProperty* pxField = new CMFCPropertyGridProperty(szName);

		Populate(pField, pxField);

		pxGroup->AddSubItem(pxField);
	}
	pFields->Release();
}

void CDataProductPropertyDdialog::Populate(ck::data::Field* pField, CMFCPropertyGridProperty* pxField)
{
	ck::data::FieldDef* pFieldDef = NULL;
	pFieldDef = pField->GetFieldDef();

	const char* szFieldName = pField->GetName();
	pxField->AddSubItem(new CMFCPropertyGridProperty("名称", (_variant_t)szFieldName, _T("")));
	pxField->AddSubItem(new CMFCPropertyGridProperty("类型", (_variant_t)ck_datatype_2_cn_string(pFieldDef->GetType()), _T("")));
	if (pFieldDef->GetType() == ck::ckDataTypeString)
	{
		char szstr[Z_PATH_MAX];
		sprintf(szstr, "%d", pFieldDef->GetLength());
		pxField->AddSubItem(new CMFCPropertyGridProperty("长度", (_variant_t)szstr, _T("")));
	}
	char szstr[Z_PATH_MAX];
	sprintf(szstr, "%d", pFieldDef->GetDecimals());
	pxField->AddSubItem(new CMFCPropertyGridProperty("精度", (_variant_t)szstr, _T("")));
	pxField->AddSubItem(new CMFCPropertyGridProperty("是否可为空", (_variant_t)(pFieldDef->IsNullable() ? "是" : "否"), _T("")));

}
